package com.zyh.controller;


import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyh.entity.Auction;
import com.zyh.entity.Record;
import com.zyh.entity.User;
import com.zyh.service.FeignUserService;
import com.zyh.service.IRecordService;
import com.zyh.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    IRecordService service;
    @Autowired
    FeignUserService feignUserService;

    @GetMapping("/")
    public List<Record> selectByAuctionId(Integer id){
        QueryWrapper<Record> wrapper = new QueryWrapper<Record>();
        wrapper.eq("auction_id",id).orderByDesc("record_price");
        List<Record> list = service.list(wrapper);
        for (Record record : list) {
            Integer userId = record.getUserId();
            User user = feignUserService.selectById(userId);
            record.setUser(user);
        }
        return list;
    }

    @PostMapping("/outPrice")
    public void outPrice(@RequestHeader("authorization")String jwt, @RequestBody Record record){
//        Boolean aBoolean = service.CheckAndSave(record);
//        if(aBoolean==true){
//            DecodedJWT verify = JwtUtils.verify(jwt);
//            String userId = verify.getClaim("userId").asString();
//            int id = Integer.parseInt(userId);
//            record.setRecordTime(LocalDateTime.now());
//            record.setUserId(id);
//            service.save(record);
//        }else{
//            throw new RuntimeException("出价不得低于当前最高价");
//        }
        record.setRecordTime(LocalDateTime.now());
        DecodedJWT verify = JwtUtils.verify(jwt);
        String useridStr = verify.getClaim("userid").asString();
        Integer userid=Integer.parseInt(useridStr);
        record.setUserId(userid);
        service.CheckAndSave(record);
    }
}

